package com.system.web;

import java.util.Arrays;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.AJAXResultMessage;
import com.common.util.DTOResponse;
import com.common.util.ExtjsPageable;
import com.syslog.aop.SystemServiceLog;
import com.system.entity.Permission;
import com.system.entity.Role;
import com.system.service.IRoleService;



@Controller
@RequestMapping(value="/role")
public class RoleController 
{
	@Autowired
	private IRoleService roleService;
	
	@SystemServiceLog("新建或修改角色基础信息")
	@RequestMapping(value="/saveOrUpdate")
	public @ResponseBody AJAXResultMessage saveOrUpdate(Role role) 
	{
		try {
			System.out.println(role.getId());
			System.out.println(role.getName());
			roleService.saveOrUpdate(role);
    		return new AJAXResultMessage(true,"操作成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new AJAXResultMessage(false,"操作失败!");
	    }
	}
	@SystemServiceLog("分配角色权限")
	@RequestMapping(value="/saveRolePermission")
	public @ResponseBody AJAXResultMessage saveRolePermission(Long rId,Long[] pIds) 
	{
		try {
			List<Long> pIdlists=Arrays.asList(pIds);
			System.out.println("rId:"+rId);
			System.out.println("pIdlists:"+pIdlists);
			roleService.saveRolePermission(rId,pIdlists);
    		return new AJAXResultMessage(true,"操作成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new AJAXResultMessage(false,"操作失败!");
	    }
	}
	@SystemServiceLog("批量删除角色")
	@RequestMapping(value = "/deleteRoles")
    public @ResponseBody AJAXResultMessage delete(Long[] ids) {
    	try {
    		List<Long> idlists=Arrays.asList(ids);
    		if(null!=idlists) {
    			roleService.delete(idlists);
    		}    		
    		return new AJAXResultMessage(true,"操作成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new AJAXResultMessage(false,"操作失败!");
	    }
    }
	@SystemServiceLog("删除角色")
	@RequestMapping(value = "/delete")
    public @ResponseBody AJAXResultMessage delete(Long id) {
    	try {
    		roleService.delete(id);
    		return new AJAXResultMessage(true,"操作成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new AJAXResultMessage(false,"操作失败!");
	    }
    }
	@RequestMapping("/findOne")
	public @ResponseBody Role findOne(Long id) {
		return roleService.findPermissionsByRoleId(id);
	}
	
	@SystemServiceLog("查找所有角色")
	@RequestMapping("/findPage")
	public @ResponseBody Page<Role> findAll(Role role,ExtjsPageable pageable) 
	{
		Page<Role> page =roleService.findPage(Role.getSpecification(role), pageable.getPageable());		
		return page;
	}
	@SystemServiceLog("查找角色拥有的权限")
	@RequestMapping("/findRolePermission")
	public @ResponseBody DTOResponse<Permission> findPermission(Long id) 
	{
		List<Permission> ps =roleService.findRolePermission(id);		
		return new DTOResponse<Permission>(ps);
	}
	
	@SystemServiceLog("查找用户拥有的角色")
	@RequestMapping("/findStaffRole")
	public @ResponseBody DTOResponse<Role> findStaffRole(Long id) 
	{
		List<Role> ps =roleService.findStaffRole(id);		
		return new DTOResponse<Role>(ps);
	}
	
	@SystemServiceLog("分配用户角色")
	@RequestMapping(value="/saveStaffRole")
	public @ResponseBody AJAXResultMessage saveStaffRole(Long staffId,Long[] rIds) 
	{
		try {
			List<Long> rIdlists=Arrays.asList(rIds);
			System.out.println("staffId:"+staffId);
			System.out.println("rIdlists:"+rIdlists);
			roleService.saveStaffRole(staffId,rIdlists);
    		return new AJAXResultMessage(true,"操作成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        return new AJAXResultMessage(false,"操作失败!");
	    }
	}
}
